using System.Collections;
using UnityEngine;

public class Entity_StatHandler : MonoBehaviour
{
    private ElementType currentEffect = ElementType.None;
    private Entity entity;
    private Entity_VFX entityVFX;
    private Entity_Health entityHealth;
    private Entity_Stat stats;

    [Header("Shock details")]
    [SerializeField] private float maxCharge = 1;
    [SerializeField] private GameObject lightningStrikeVFX;
    private float currentCharge;
    private Coroutine shockCoroutine;
    void Awake()
    {
        entity = GetComponent<Entity>();
        entityVFX = GetComponent<Entity_VFX>();
        stats = GetComponent<Entity_Stat>();
        entityHealth = GetComponent<Entity_Health>();
    }

    public void RemoveAllNegativeEffects()
    {
        StopAllCoroutines();
        currentEffect = ElementType.None;
        entity.StopSlowdownEntityCo();
        entityVFX.StopAllVFXCoroutine();
    }
    //根据元素种类执行状态效果
    public void ApplyStatusEffect(ElementType elementType, ElementalEffectData effectData)
    {
        if (elementType == ElementType.Ice && CanBeApplied(ElementType.Ice))
            ApplyChilledEffect(effectData.chillDuration, effectData.chillSlowMultiplier);
        else if (elementType == ElementType.Fire && CanBeApplied(ElementType.Fire))
            ApplyBurnEffect(effectData.burnDuration, effectData.burnDamage);
        else if (elementType == ElementType.Lightning && CanBeApplied(ElementType.Lightning))
            ApplyShockEffect(effectData.shockDuration, effectData.shockDamage, effectData.shockCharge);
    }


    public bool CanBeApplied(ElementType elementType)
    {
        if (elementType == ElementType.Lightning && currentEffect == ElementType.Lightning)
            return true;
        return currentEffect == ElementType.None;
    }

    //运用冰元素造成的减速效果
    private void ApplyChilledEffect(float duration, float slowMultiplier)
    {
        //获得自身冰抗，冰抗能够衰减自身所受到的减速时间
        float iceResistance = stats.GetElementalResistance(ElementType.Ice);
        //衰减后的减速时间
        float finalDuration = duration * (1 - iceResistance);
        StartCoroutine(ChillEffectCo(finalDuration, slowMultiplier));
    }
    private IEnumerator ChillEffectCo(float duration, float slowMultiplier)
    {
        //将当前受到的元素种类设置为冰元素种类
        currentEffect = ElementType.Ice;
        //衰减自身移动速度等数据
        entity.StartSlowdownEntityCo(duration, slowMultiplier);
        //运行受到元素攻击时的特效
        entityVFX.PlayOnStatusVFX(duration, currentEffect);

        yield return new WaitForSeconds(duration);

        //将当前受到的元素种类设置为无
        currentEffect = ElementType.None;
    }
    //运用火元素持续烧伤的效果
    private void ApplyBurnEffect(float duration, float fireDamage)
    {
        float fireResistance = stats.GetElementalResistance(ElementType.Fire);
        float finalDamage = fireDamage * (1 - fireResistance);
        StartCoroutine(BurnEffectCo(duration, finalDamage));
    }
    private IEnumerator BurnEffectCo(float duration, float totalDamage)
    {
        //将当前受到的元素种类设置为火元素种类
        currentEffect = ElementType.Fire;
        entityVFX.PlayOnStatusVFX(duration, currentEffect);
        //每设置乘数，当乘数为2时，火元素烧伤伤害次数为默认参数3的2倍共6次，每次烧伤后等待0.5s，共计3s，为默认参数3s
        int tickPerSecond = 2;
        int tickCount = Mathf.RoundToInt(tickPerSecond * duration);
        float damagePerTick = totalDamage / tickCount;
        float tickInterval = 1f / tickPerSecond;
        for (int i = 0; i < tickCount; i++)
        {
            //扣除烧伤伤害
            entityHealth.ReduceHealth(damagePerTick);
            yield return new WaitForSeconds(tickInterval);
        }
        currentEffect = ElementType.None;
    }
    //运用雷元素发生电击的效果
    private void ApplyShockEffect(float duration, float damage, float charge)
    {
        float lightningResistance = stats.GetElementalResistance(ElementType.Lightning);
        float finalCharge = charge * (1 - lightningResistance);
        currentCharge += finalCharge;
        //如果当前充能大于充能阈值
        if (currentCharge > maxCharge)
        {
            DoLightningStrike(damage);
            //更新充能机制
            StopShockEffect();
            return;
        }
        if (shockCoroutine != null)
            StopCoroutine(ShockEffectCo(duration));
        shockCoroutine = StartCoroutine(ShockEffectCo(duration));
    }
    private IEnumerator ShockEffectCo(float duration)
    {
        currentEffect = ElementType.Lightning;
        entityVFX.PlayOnStatusVFX(duration, ElementType.Lightning);
        yield return new WaitForSeconds(duration);
        StopShockEffect();
    }
    //雷击效果
    private void DoLightningStrike(float damage)
    {
        GameObject lightningStrikeVFXPrefab = PoolManager.Instance.Release(lightningStrikeVFX, (explodePrefab) =>
        {
            explodePrefab.transform.position = transform.position;
            explodePrefab.transform.rotation = Quaternion.identity;
        });
        entityHealth.ReduceHealth(damage);
    }
    private void StopShockEffect()
    {
        currentEffect = ElementType.None;
        currentCharge = 0;
        entityVFX.StopAllVFXCoroutine();
    }
}
